package com.eventur;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapViewActivity extends MapActivity{
	private MapView mapView = null;
	//private MapController mc;

	private static final int MENU_HOME = Menu.FIRST;
	//private static final int MENU_QUIT = Menu.FIRST+1;

	private GeoPoint p;
	private double currentLatitude;
	private double currentLongitude;
	double eventLatitude ;
	double eventLongitude;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0,MENU_HOME,0,"Home");
		//menu.add(0,MENU_QUIT,1,"Quit");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()	){
		case MENU_HOME: Intent intent = new Intent(this, MainActivity.class);;
		startActivity(intent);this.finish();
		return true;
		//case MENU_QUIT: int pid = android.os.Process.myPid();
		//android.os.Process.killProcess(pid); finish();
		//return true;
		}
		return false;

	}
	@Override
	protected void onResume(){
		super.onResume();
	}
	
	@Override
	protected void onPause(){
		super.onPause();	
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		mapView = (MapView) findViewById(R.id.mapview1);
		//mc = mapView.getController();


		Bundle bundle = getIntent().getExtras();
		

		if (bundle != null) {
			currentLatitude = bundle.getDouble("currentLatitude");
			currentLongitude = bundle.getDouble("currentLongitude");
			
			
			System.out.println("currentLatlong in mapactivity:"+currentLatitude+" --"+currentLongitude);
			Drawable myLocationMarker=getResources().getDrawable(R.drawable.mylocationmarker);
			myLocationMarker.setBounds(0, 0, myLocationMarker.getIntrinsicWidth(),
					myLocationMarker.getIntrinsicHeight());
			System.out.println("done collection current lat lon");
			if(bundle.getString("key").equalsIgnoreCase("single")){
				eventLatitude = bundle.getDouble("latitude");
				eventLongitude = bundle.getDouble("longitude");


				if(eventLatitude==eventLongitude || eventLatitude==0.0 || eventLongitude==0.0){
					eventLatitude =40.440;
					eventLongitude= -79.996;
				}
			
				System.out.println("setting cente on event lat/lon");
				mapView.getController().setCenter(getPoint(eventLatitude,eventLongitude));
				mapView.getController().setZoom(12);
				mapView.setBuiltInZoomControls(true);

				Drawable marker=getResources().getDrawable(R.drawable.marker);
				marker.setBounds(0, 0, marker.getIntrinsicWidth(),
						marker.getIntrinsicHeight());

				mapView.getController().setZoom(12);	
				mapView.setBuiltInZoomControls(true);
				mapView.getOverlays().add(new SitesOverlay(marker,"event"));
				System.out.println("displaying my location now");
				mapView.getOverlays().add(new SitesOverlay(myLocationMarker,"myLocation"));

			 
			}
			else{
				System.out.println("entered map");
				mapView.getController().setCenter(getPoint(40.443299,-79.948573));
				mapView.getController().setZoom(12);
				mapView.setBuiltInZoomControls(true);

				Drawable todayMarker=getResources().getDrawable(R.drawable.todaymarker);
				todayMarker.setBounds(0, 0, todayMarker.getIntrinsicWidth(),
						todayMarker.getIntrinsicHeight());


				Drawable tomorrowMarker=getResources().getDrawable(R.drawable.tomorrowmarker);
				tomorrowMarker.setBounds(0, 0, tomorrowMarker.getIntrinsicWidth(),
						tomorrowMarker.getIntrinsicHeight());
				
				Drawable futureMarker=getResources().getDrawable(R.drawable.futuremarker);
				futureMarker.setBounds(0, 0, futureMarker.getIntrinsicWidth(),
						futureMarker.getIntrinsicHeight());

				
				mapView.getOverlays().add(new SitesOverlay(futureMarker,"week"));
				mapView.getOverlays().add(new SitesOverlay(tomorrowMarker,"tomorrow"));
				mapView.getOverlays().add(new SitesOverlay(todayMarker,"today"));
				//mapView.getOverlays().add(new MapOverlay());
				//me=new MyLocationOverlay(this, mapView);
				//mapView.getOverlays().add(me);



			}
			mapView.getOverlays().add(new SitesOverlay(myLocationMarker,"myLocation"));
		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	private GeoPoint getPoint(double lat, double lon) {
		return(new GeoPoint((int)(lat*1000000.0),(int)(lon*1000000.0)));
	}


	private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> items=new ArrayList<OverlayItem>();
		@SuppressWarnings("unused")
		private Drawable marker=null;
		@SuppressWarnings("unused")
		private String flag = null;
		public SitesOverlay(Drawable marker,String flag) {
			super(marker);
			this.marker=marker;
			this.flag = flag;
			boundCenterBottom(marker);
			Event event = new Event();
			if(flag.equalsIgnoreCase("today")){
				for(int i=0;i<XmlParser.todayEvents.size();i++){
					event = XmlParser.todayEvents.get(i);
					if(event.getLatitude()!=0.0 || event.getLongitude()!=0.0)
					{
						//System.out.println("today marker:"+event.getLatitude());
						items.add(new OverlayItem(getPoint(event.getLatitude(),
								event.getLongitude()),
								event.getTitle(), event.getTitle()+"\n on "+event.getsDate()+"\n at "+event.getVenue()));
					}
				}
				System.out.println("size of list:"+items.size());
			}
			else if(flag.equalsIgnoreCase("tomorrow")){
				for(int i=0;i<XmlParser.tomorrowEvents.size();i++){
					event = XmlParser.tomorrowEvents.get(i);
					if(event.getLatitude()!=0.0 || event.getLongitude()!=0.0)
					{
						items.add(new OverlayItem(getPoint(event.getLatitude(),
								event.getLongitude()),
								event.getTitle(), event.getTitle()+"\n on "+event.getsDate()+"\n at "+event.getVenue()));
					}
				}
				System.out.println("size of list:"+items.size());

			}
			else if(flag.equalsIgnoreCase("week")){
				for(int i=0;i<XmlParser.weekEvents.size();i++){
					event = XmlParser.weekEvents.get(i);
					if(event.getLatitude()!=0.0 || event.getLongitude()!=0.0)
					{
						items.add(new OverlayItem(getPoint(event.getLatitude(),
								event.getLongitude()),
								event.getTitle(), event.getTitle()+"\n on "+event.getsDate()+"\n at "+event.getVenue()));
					}
				}
				System.out.println("size of list:"+items.size());

			}
			else if(flag.equalsIgnoreCase("myLocation")){
				System.out.println("current Location to be mapped now:"+currentLatitude+" "+currentLongitude);
				items.add(new OverlayItem(getPoint(currentLatitude,
						currentLongitude),
						"My Location", "My Location"));
			}
			else if(flag.equalsIgnoreCase("event")){
				System.out.println("event Location to be mapped:"+eventLatitude+" "+eventLongitude);
				items.add(new OverlayItem(getPoint(eventLatitude,
						eventLongitude),
						"Event Location", "Event Location"));
			}
	
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return(items.get(i));
		}

		@Override
		protected boolean onTap(int i) {
			Toast.makeText(MapViewActivity.this,
					items.get(i).getSnippet(),
					Toast.LENGTH_LONG).show();

			return(true);
		}

		@Override
		public int size() {
			return(items.size());
		}
	}


	class MapOverlay extends com.google.android.maps.Overlay
	{
		@Override
		public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when) 
		{
			super.draw(canvas, mapView, shadow);                   

			//---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(p, screenPts);

			//---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(
					getResources(), R.drawable.marker);            
			canvas.drawBitmap(bmp, screenPts.x-25, screenPts.y-25, null);         
			return true;
		}
	} 
}
